<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Models\OpenShop\InvestmentPromotion\OpenShopEmployeeNotice;
use App\Service\OpenShop\Feishu\PermissionService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Http\Request;

class NoticeController extends BaseController
{
    public function index(Request $request)
    {
        $scope         = PermissionService::hasAll();
        $read      = (int)$request->get('read', 0);
        $pageNum   = (int)$request->get('pageNum', 1);
        $pageSize  = (int)$request->get('pageSize', 20);
        $paginator = OpenShopEmployeeNotice::query()
            ->when($scope, function ($query) use ($scope) {
                $query->where('userId', $scope);
            })
            ->where('read', $read)
            ->with('signOrder')
            ->orderBy('id', 'desc')
            ->paginate($pageSize, ['*'], 'pageNum', $pageNum);
        $body = RmsUserService::getRmsUserInfoByUserIds($paginator->pluck('userId')->toArray());
        $list      = $paginator->map(function (OpenShopEmployeeNotice $notice) use ($body) {
            $orderNo = (string)optional($notice->signOrder)->orderNo;
            $owner = '';
            foreach (($body['data'] ?? []) as $user) {
                if ($user['id'] == $notice->userId) {
                    $owner = $user['username'];
                }
            }
            return [
                'id'           => $notice->id,
                'orderNo'      => $orderNo,
                'content'      => $notice->content,
                'orderId'      => $notice->orderId,
                'owner'        => $owner,
                'operatorName' => $notice->operatorName,
                'createdAt'    => $notice->createdAt->format('Y-m-d H:i'),
                'shopCode'     => $notice->shopCode,
                'shopName'     => $notice->shopName,
                'read'         => $notice->read,
                'readAt'       => $notice->readAt->format('Y-m-d H:i'),
            ];
        });
        if ($read) {
            $readNum   = $paginator->total();
            $unreadNum = OpenShopEmployeeNotice::query()
                ->when($scope, function ($query) use ($scope) {
                    $query->where('userId', $scope);
                })
                ->where('read', 0)
                ->count();
        } else {
            $readNum   = OpenShopEmployeeNotice::query()
                ->when($scope, function ($query) use ($scope) {
                    $query->where('userId', $scope);
                })
                ->where('read', 1)
                ->count();
            $unreadNum = $paginator->total();
        }
        return $this->success([
            'list'      => $list,
            'count'     => $paginator->total(),
            'readNum'   => $readNum,
            'unreadNum' => $unreadNum,
            'pageNum'   => $pageNum,
            'pageSize'  => $pageSize,
        ]);
    }

    public function read(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|int',
        ]);
        $id       = (int)$request->id;
        OpenShopEmployeeNotice::query()
            ->where('id', $id)
            ->where('userId', getRmsUserId())
            ->update([
                'read'      => 1,
                'readAt'    => time(),
                'updatedAt' => time(),
            ]);
        return $this->success();
    }
}
